<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets" 
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        
        <ui:composition template="../../templates/master.xhtml">
            <ui:define name="content">
                <p:toolbar>
                    <p:toolbarGroup>
                        <p:commandButton icon="ui-icon-plus" value="Add new" action="#{categoriesController.prepareCreate()}"/>
                    </p:toolbarGroup>
                </p:toolbar>
                <br/>
                <p:dataTable  value="#{categoriesController.cats}" paginator="true" rows="10"
                             paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}" 
                             var="c" rowsPerPageTemplate="5,10,15" id="carTable" lazy="true">
                    <p:column styleClass="row-item-#{c.id}" headerText="ID">
                        <h:outputText value="#{c.id}"/>
                    </p:column>
                    <p:column sortBy="#{c.name}" filterBy="#{c.name}" headerText="Name">
                        <h:outputText value="#{c.name}"/>
                    </p:column>
                    <p:column headerText="Description">
                        <h:outputText value="#{c.description}"/>
                    </p:column>
                  
                    <p:column headerText="#">
                        <p:commandButton oncomplete="catDialog.show()" update=":form:catDetial" id="ViewDetailButton" title="View" icon="ui-icon-search">
                            <f:setPropertyActionListener value="#{c}" target="#{categoriesController.selected}"/>
                        </p:commandButton>
                        <p:commandButton id="EditButton" immediate="true" action="#{categoriesController.prepareEdit()}" title="Edit" icon="ui-icon-pencil">
                            <f:setPropertyActionListener value="#{c}" target="#{categoriesController.selected}"/>
                        </p:commandButton>
                        <p:commandButton oncomplete="onDeleteRowComplete(xhr,status,args)" id="DeleteButton" title="Delete" action="#{categoriesController.destroy()}" icon="ui-icon-close">
                            <f:setPropertyActionListener value="#{c}" target="#{categoriesController.selected}"/>
                        </p:commandButton>


                    </p:column>

                </p:dataTable>
                <p:dialog header="Category Detail" widgetVar="catDialog" resizable="false" id="carDlg"  
                          showEffect="fade" hideEffect="explode" modal="true">  

                    <h:panelGrid id="catDetial" columns="2" cellpadding="4" style="margin:0 auto;">  

                        <f:facet name="header">  
                            <p:graphicImage value="#{categoriesController.selected.icon}"/>  
                        </f:facet>  

                        <h:outputText value="Name:" />  
                        <h:outputText value="#{categoriesController.selected.name}" style="font-weight:bold"/>  

                        <h:outputText value="Description: " />  
                        <h:outputText value="#{categoriesController.selected.description}" style="font-weight:bold"/>  

                    </h:panelGrid>  

                </p:dialog>  
            </ui:define>
            <ui:define name="footer">
                <ui:include src="../../templates/footer.xhtml"/>
                <script type="text/javascript">
                    function onDeleteRowComplete(xhr, status,args){
                        var item = args.item;
                        $("td.row-item-"+item).closest("tr").fadeOut("fast").remove();
                        console.log(item);
                    }
                </script>
            </ui:define>
        </ui:composition>
    </h:body>
</html>

